package infostroy.bookagolf.components.widgets;

import infostroy.bookagolf.common.Constants.Colors;
import infostroy.bookagolf.components.borders.RoundedBorder;
import infostroy.bookagolf.components.panels.ScrollPanel;

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * Created by valer_000 on 2/13/14.
 */
public class GolfGallery extends JPanel {

    /**
	 * 
	 */
	private static final long serialVersionUID = -541281967578227532L;
	private static final Color DEFAULT_BACKGROUND = Color.white;
    private static final Dimension IMAGE_MAX_SIZE = new Dimension(100, 90);

    private JPanel imagesHolder;
    private JScrollPane scrollPane;

    public GolfGallery() {
        RoundedBorder outer = new RoundedBorder();
        EmptyBorder inner = new EmptyBorder(5, 10, 5 , 10);
        this.setBorder(new CompoundBorder(outer, inner));
        this.initGallery();
    }

    private void initGallery() {
        this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
        imagesHolder = new JPanel();
        imagesHolder.setBackground(Colors.WHITE);
        imagesHolder.setLayout(new BoxLayout(imagesHolder, BoxLayout.LINE_AXIS));        
        scrollPane = new ScrollPanel(imagesHolder);
        scrollPane.setBorder(null);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

        this.add(scrollPane);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g;

        RenderingHints hints = graphics.getRenderingHints();
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setColor(DEFAULT_BACKGROUND);
        graphics.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 10);
        graphics.setRenderingHints(hints);
    }

    //TODO:(Valera) init size in dimensions
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(super.getMaximumSize().width, 80);
    }

    @Override
    public Dimension getMaximumSize() {
        return new Dimension(super.getMaximumSize().width, 130);
    }

    public void addImage(BufferedImage image) {
        if (image == null) {
            throw new NullPointerException();
        }
        JLabel photo = new JLabel();
        photo.setAlignmentY(Component.BOTTOM_ALIGNMENT);
        if (image.getWidth() > IMAGE_MAX_SIZE.width
                || image.getHeight() > IMAGE_MAX_SIZE.getHeight()) {
            photo.setIcon(new ImageIcon(resizeImage(image)));
            photo.setMaximumSize(IMAGE_MAX_SIZE);
        } else {
            photo.setIcon(new ImageIcon(image));
            photo.setMaximumSize(new Dimension(image.getWidth(), image.getHeight()));
        }

        this.imagesHolder.add(photo);
        this.imagesHolder.add(Box.createRigidArea(new Dimension(5, 5)));
    }

    private BufferedImage resizeImage(BufferedImage image) {
        BufferedImage resizedImage = new BufferedImage(IMAGE_MAX_SIZE.width
                , IMAGE_MAX_SIZE.height, image.getType());
        Graphics2D g = resizedImage.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g.setComposite(AlphaComposite.Src);
        g.drawImage(image, 0, 0, IMAGE_MAX_SIZE.width
                , IMAGE_MAX_SIZE.height, null);
        g.dispose();

        return resizedImage;
    }

    public void cleanImageGallery(){
        imagesHolder.removeAll();
        imagesHolder.revalidate();
    }

}
